/*
 * This is my temporary hacking program and should not be used for anything!
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "reader.h"

int configure_raw( int fd ) {
  char send[64];

  /*LRC Disable*/
  send[0] = 0x53; /* L */
  send[1] = 0x43; /* C */
  send[2] = 0x44; /* D */
  int w = write( fd, send, 3 );
  printf( "Wrote len %i\n", w );


  /*LRC Disable*/
  send[0] = 0x55; /* U */
  send[1] = 0x56; /* V */
  send[2] = 0x57; /* W */
  w = write( fd, send, 3 );
  printf( "Wrote len %i\n", w );

  while ( 1 ) {
    int r = read( fd, send, sizeof(send ) );
    printf( "Read %i\n", r );
    send[r] = '\0';
    printf( "Read: %s\n", send );
    
  }

  return 0;

}


/*
 * Sample main program for the card reader API that I wrote!
 */
int main( int argc, char *argv[] ) {
  char *card = (char*)malloc(READ_BUFFER_SIZE*sizeof(char));

  /* Open the port to the reader */
  int fd = get_device_port();
  if ( fd == -1 ) return 1;

  if ( configure_raw( fd ) != 0 ) {
    fprintf( stderr, "NOES\n" );
  }

  /*Read from the device*/
  int card_len = read_card( fd, &card, READ_BUFFER_SIZE*sizeof(char) );   
 
  printf( "CARD(string) = %s\n", card );
  print_card_by_raw_bytes( card, card_len );
  print_card_by_values( card, card_len, 0 );
  print_card_by_values( card, card_len, 1 );
  
  /*Close the device port*/
  close_device_port( fd );

  free(card);

  return 0;
}
